package com.zj.fy;

/**
 * Created by Even on 2017/2/20.
 */
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zj.fy.DB.SdNoticeEntity;
import com.zj.fy.DB.sdDao;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.JsonPathSelector;
import us.codecraft.webmagic.utils.HttpConstant;

public class sd_notice implements PageProcessor {
    private static String username = "NoticeSDList"; 
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);
    public Site getSite() {
        return site;
    }
    public void process(Page page) {
        List<String> Notice_SD_ID = new JsonPathSelector("$.list[*].Notice_SD_ID").selectList(page.getRawText());
        List<String> CaseNo = new JsonPathSelector("$.list[*].CaseNo").selectList(page.getRawText());
        List<String> Content = new JsonPathSelector("$.list[*].Content").selectList(page.getRawText());
        List<String> Court = new JsonPathSelector("$.list[*].Court").selectList(page.getRawText());
        List<String> CreateTime = new JsonPathSelector("$.list[*].Notice_SD_ID").selectList(page.getRawText());
        for (int j=0;j<10;j++) {
            SdNoticeEntity sdNotice = new SdNoticeEntity();
            sdNotice.setCaseNo(CaseNo.get(j));
            sdNotice.setNoticeSdId(Notice_SD_ID.get(j));
            sdNotice.setCourt(Court.get(j));
            sdNotice.setContent(Content.get(j));
            sdNotice.setPulishTime(CreateTime.get(j));
            new sdDao().add(sdNotice);
        }
    }

    public static void main(String[] args) {
        PageProcessor pageProcessor = new sd_notice();
        Spider spider = Spider.create(pageProcessor);
        for (int i = 1; i < 10; i++) {
            String url = null;
            Map<String, Object> nameValuePair = new HashMap<String, Object>();
            NameValuePair[] values = new NameValuePair[3];
            values[0] = new BasicNameValuePair("pagesize", "10");
            values[1] = new BasicNameValuePair("cbfy", "");
            values[2] = new BasicNameValuePair("pageno", String.valueOf(i));
            nameValuePair.put("nameValuePair", values);
            url = "http://www.zjsfgkw.cn/Notice/NoticeSD?cbfy=&pageno="+String.valueOf(i)+"&pagesize=10";
            Request request = new Request(url);
            request.setExtras(nameValuePair);
            request.setMethod(HttpConstant.Method.POST);
            spider.addRequest(request);
        }
        spider.thread(3).run();
    }
}
